<?php
    require_once(dirname(__FILE__).'/include/config.inc.php');
    require_once(dirname(__FILE__).'/include/mysqli.class.php');
    $mysql = new MySql();

    /*退款表单提交*/
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $date = array(
            'uid'       => $_POST['u_id'],
            'order_id'  => $_POST['order_id'],
            'shop_id'   => $_POST['shop_id'],
            'goods_id'  => $_POST['goods_id'],
            'bank_id'  => $_POST['bank'],
            'money'     => $_POST['money'],
            'reason'    => $_POST['reason'],
            'refundnum'  => mt_rand(10,100).time().mt_rand(10,1000)
        );
        $order =$mysql->GetOne("select * from dyspw_goods_order  where id = {$date['order_id']} ");
        $refund = $mysql->GetOne("select * from dyspw_refund  where order_id = {$date['order_id']} ");
        if($order['state'] == 0 || $order['state'] == 3){
            $result['status'] = 'n';
            $result['info'] = '该订单未付款或订单已核销';
            exit(json_encode($result));
        }
        if(!empty($refund)){
            $result['status'] = 'n';
            $result['info'] = '该订单已发起退款';
            exit(json_encode($result));
        }
        $time = time();
        $add_refund = $mysql->ExecNoneQuery("INSERT INTO dyspw_refund (uid, order_id, shop_id, goods_id, `money`, `time`, reason, bank_id, refundnum) VALUES ({$date['uid']},{$date['order_id']},{$date['shop_id']},{$date['goods_id']},{$date['money']},'{$time}','{$date['reason']}' ,'{$date['bank_id']}','{$date['refundnum']}')");

        if($add_refund){
            $mysql->ExecNoneQuery("update dyspw_goods_order set state = 5 where id = {$order['id']}");
            // require_once "api/wxRefund.php";
            // $wxRefund = new wxRefund();
            // $result = $wxRefund->SubmitJsApiWxRefund($order['transaction_id'],$order['ordernum'],$date['refundnum'],$order['money'],$order['money']);
            // print_r($result);exit;
            // if($result['return_code'] == 'SUCCESS'){
            //     if($result['result_code'] == 'SUCCESS'){
            //         $mysql->ExecNoneQuery("update dyspw_goods_order set state = 6 where id = {$order['id']}");
            //         $mysql->ExecNoneQuery("update dyspw_refund set state = 1 where refundnum = {$date['refundnum']}");
            //         $result['status'] = 'y';
            //         $result['info'] = '退款发起成功，微信将在一个工作日内退款至您的账户';
            //         exit(json_encode($result));
            //     }elseif ($result['result_code'] == 'FAIL') {
            //         $result['status'] = 'n';
            //         $result['info'] = $result['err_code_des'];
            //         exit(json_encode($result));
            //     }
            // }else{
            //     $result['status'] = 'y';
            //     $result['info'] = '退款请求失败，请重新提交';
            //     exit(json_encode($result));
            // }
            $result['status'] = 'y';
            $result['info'] = '退款提交完成，等待审核';
            exit(json_encode($result));
        }else{
            $result['status'] = 'n';
            $result['info'] = '退款提交失败，请重新提交';
            exit(json_encode($result));
        }
    }


 ?>
